﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GameView.Comment;
using GameView.Models;
using GameView.Services.Comment;
using GameView.Services.Dtos.WithdrawalDto;
using System.Collections.ObjectModel;

namespace GameView.ViewModels
{
    public partial class StatisticsByProjectsViewModel : ObservableObject
    {
        private StatisticsService _service;

        public StatisticsByProjectsViewModel(StatisticsService service)
        {
            _service = service;
            LoadDataAsync().ConfigureAwait(false);
        }

        [ObservableProperty]
        private ObservableCollection<StatisticsEntity> _listOne = [];

        [ObservableProperty]
        private ObservableCollection<StatisticsEntity> _listTwo = [];

        [ObservableProperty]
        private ObservableCollection<StatisticsEntity> _listThree = [];

        [ObservableProperty]
        private bool _isFeatureEnabled = false;

        [ObservableProperty]
        private WithdrawalQueryDto _queryDto = new();

        [ObservableProperty]
        private bool _isLoading = false;

        [ObservableProperty]
        private bool _isByProject = true;

        [ObservableProperty]
        private bool _isByDay = false;

        [ObservableProperty]
        private long _recharge = 0;

        [ObservableProperty]
        private long _withdrawal = 0;

        [ObservableProperty]
        private string _mainName = "项目统计";

        [ObservableProperty]
        private string _detailName1 = string.Empty;

        [ObservableProperty]
        private string _detailName2 = string.Empty;

        [RelayCommand]
        private async Task RowSelected(StatisticsEntity entity)
        {
            await selectedChangeAsync(entity);
        }

        private async Task LoadDataAsync()
        {
            IsLoading = true;
            await Task.Delay(200);
            try
            {
                ListTwo.Clear();
                ListThree.Clear();
                QueryDto.ProjectName = string.Empty;
                if (IsByDay)
                {
                    QueryDto.EndTimeDT = QueryDto.StartTimeDT;
                }
                if (IsByProject)
                {
                    MainName = "项目统计";
                    DetailName1 = $"大区统计";
                    DetailName2 = $"商行统计";
                    var p = await _service.GetByProjectAsync(QueryDto);
                    if (p.Data.IsNullOrEmpty())
                        return;
                    ListOne = [.. p.Data];
                }
                else
                {
                    MainName = "商行统计";
                    DetailName1 = $"大区统计";
                    DetailName2 = $"玩家统计";
                    var p = await _service.GetByMerchantAsync(QueryDto);
                    ListOne = [.. p.Data];
                }
                if (ListOne != null && ListOne.Count > 0)
                {
                    Recharge = ListOne.Sum(q => q.Recharge);
                    Withdrawal = ListOne.Sum(q => q.Withdrawal);

                    await selectedChangeAsync(ListOne[0]);
                }
            }
            catch (Exception ex)
            {
                GlobalMessage.Instance.ShowError($"数据加载错误：{ex.Message}");
            }
            finally
            { IsLoading = false; }
        }

        private async Task selectedChangeAsync(StatisticsEntity entity)
        {
            if (entity == null)
                return;
            ListTwo.Clear();
            ListThree.Clear();
            DetailName1 = $"大区统计";
            DetailName2 = $"商行统计";
            List<StatisticsEntity> res = [];

            if (IsByProject)
            {
                QueryDto.ProjectName = entity.Name;
                var p = await _service.GetProjectDetailAsync(QueryDto);
                res = p.Data;

                DetailName1 = $"{entity.Name}-{DetailName1}";
                DetailName2 = $"{entity.Name}-{DetailName2}";
            }
            else
            {
                QueryDto.Merchant = entity.Name;
                var p = await _service.GetMerchantDetailAsync(QueryDto);
                res = p.Data;
                DetailName1 = $"{entity.Name}-{DetailName1}";
                DetailName2 = $"{entity.Name}-玩家统计";
            }
            if (res == null || res.Count <= 0)
                return;
            ListTwo = [.. res.FindAll(q => q.Id == 1)];
            ListThree = [.. res.FindAll(q => q.Id == 2)];
        }

        [RelayCommand]
        private async Task SearchAsync()
        {
            await LoadDataAsync();
        }
    }
}